package com.ivo.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.ContextLoaderListener;

import com.ivo.security.config.SecurityConfig;
import com.ivo.security.config.SecurityConfigFactory;

/**
 * <p>
 * StartupListener class used to initialize and database settings and populate
 * any application-wide drop-downs.
 * 
 * <p>
 * Keep in mind that this listener is executed outside of
 * OpenSessionInViewFilter, so if you're using Hibernate you'll have to
 * explicitly initialize all loaded data at the Dao or service level to avoid
 * LazyInitializationException. Hibernate.initialize() works well for doing
 * this.
 * 
 * @author David Yang
 */
public class StartupListener extends ContextLoaderListener implements ServletContextListener
{
	private Log log = LogFactory.getLog( this.getClass() );

	public void contextInitialized(ServletContextEvent event)
	{
		if( log.isDebugEnabled() ) {
			log.debug( "initializing context..." );
		}

		// call Spring's context ContextLoaderListener to initialize
		// all the context files specified in web.xml
		super.contextInitialized( event );

		ServletContext context = event.getServletContext();
		/*
		 * ApplicationContext ctx =
		 * WebApplicationContextUtils.getRequiredWebApplicationContext(context);
		 * 
		 * boolean encryptPassword = false; try { ProviderManager provider =
		 * (ProviderManager) ctx.getBean("authenticationManager"); for (Iterator
		 * it = provider.getProviders().iterator(); it.hasNext();) {
		 * AuthenticationProvider p = (AuthenticationProvider) it.next(); if (p
		 * instanceof RememberMeAuthenticationProvider) {
		 * config.put("rememberMeEnabled", Boolean.TRUE); } }
		 * 
		 * if (ctx.containsBean("passwordEncoder")) { encryptPassword = true;
		 * config.put(Constants.ENCRYPT_PASSWORD, Boolean.TRUE); String
		 * algorithm = "SHA"; config.put(Constants.ENC_ALGORITHM, algorithm); } }
		 * catch (NoSuchBeanDefinitionException n) { // ignore, should only
		 * happen when testing }
		 */

		// output the retrieved values for the Init and Context Parameters
		if( log.isDebugEnabled() ) {
			/*
			 * log.debug("Remember Me Enabled? " +
			 * config.get("rememberMeEnabled")); log.debug("Encrypt Passwords? " +
			 * encryptPassword); if (encryptPassword) { log.debug("Encryption
			 * Algorithm: " + config.get(Constants.ENC_ALGORITHM)); }
			 */
			log.debug( "Populating drop-downs..." );
		}

		setupContext( context );
	}

	public void setupContext(ServletContext context)
	{
		// LookupManager mgr = (LookupManager) SpringUtil.getBean(context,
		// "lookupManager");
		// get list of possible roles
		// context.setAttribute(Constants.AVAILABLE_ROLES, mgr.getAllRoles());

		// set SecurityConfig
		String configFileLocation = null;

		if( context.getInitParameter( "security.config.file" ) != null ) {
			configFileLocation = context.getInitParameter( "security.config.file" );
			log.debug( "Security config file location: " + configFileLocation );
		}
		SecurityConfig securityConfig = SecurityConfigFactory.getInstance( configFileLocation );
		// securityConfig.addService((SecurityService)
		// SpringUtil.getBean(context, "pathService"));
		context.setAttribute( SecurityConfig.STORAGE_KEY, securityConfig );

		if( log.isDebugEnabled() ) {
			log.debug( "Drop-down initialization complete [OK]" );
		}
	}
}
